package com.mdc.mansion.api.web.modules.basedata;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.mdc.mansion.common.util.DateUtils;
import com.mdc.mansion.core.modules.basedata.dto.PushMessageDTO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.basedata.PushMessageService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.basedata.entity.PushMessage;

/**<p>com.mdc.mansion.api.web.modules.basedata.PushMessageController</p>
 * <p>Title: PushMessageController</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * @version 1.0
 */
@Controller
@RequestMapping(value = "${adminPath}/pushmessage")
public class PushMessageController extends BaseController {

    @Resource
    private PushMessageService pushMessageService;
    @RequestMapping("/list")
    public Result list(){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        PushMessage pushMessage = new PushMessage();
        pushMessage.setRestaurantId(ContextHolder.currentRestaurantId());
        Page<PushMessage> page = new Page<PushMessage>(1,10);
        String nowDate = DateUtils.formatShortDate(Calendar.getInstance().getTime());
        page.setOrderBy("a.create_time desc");
        pushMessage.setPage(page);
        List<PushMessage> messageList = pushMessageService.findList(pushMessage);
        List<PushMessageDTO> messageDTOs= Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(messageList)){
            for(PushMessage pMes:messageList){
                pMes.setSendTime(DateUtils.format(pMes.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
                /****只展示当天的消息，如果不是当天的消息则放弃******/
                if(!nowDate.equals(DateUtils.formatShortDate(pMes.getCreateTime())))
                {
                	continue;
                }
                
                PushMessageDTO pushMessageDTO=new PushMessageDTO();
                BeanUtils.copyProperties(pMes,pushMessageDTO);
                pushMessageDTO.setMes(JSONObject.parseObject(pMes.getMessage()));
                messageDTOs.add(pushMessageDTO);
            }
        }
        result.setProperty("messageList", messageDTOs);
        return result;
    }
}
